package com.cloud.message.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.message.entity.SysImGroup;
import com.cloud.message.entity.SysImGroupUser;
import com.cloud.message.service.ISysImGroupService;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * IM消息群组
 *
 * @author yzj
 * @since 2024-01-09
 */
@Tag(name = "IM消息群组")
@RestController
@RequestMapping("/message/group")
public class SysImGroupController extends BaseController {

    @Autowired
    private ISysImGroupService sysImGroupService;

    @Operation(summary = "IM消息群组分页")
    @GetMapping("/list")
    public Result list(SysImGroup imGroup) {
        startPage();
        imGroup.setUserId(SecurityUtils.getUserId());
        List<SysImGroup> sysImGroupList = sysImGroupService.getSysImGroupList(imGroup);
        return getDataTable(sysImGroupList);
    }

    @Operation(summary = "新增IM消息群组")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysImGroup imGroup) {
        imGroup.setMaster(SecurityUtils.getUserId());
        return sysImGroupService.saveSysImGroup(imGroup);
    }

    @Operation(summary = "修改IM消息群组")
    @PostMapping("/update")
    public Result update(@RequestBody SysImGroup imGroup) {
        imGroup.setMaster(SecurityUtils.getUserId());
        return sysImGroupService.editSysImGroup(imGroup);
    }

    @Operation(summary = "IM消息群组详情")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysImGroupService.getSysImGroupById(id));
    }

    @Operation(summary = "删除IM消息群组")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<Long> list = ToolUtils.setIdsToList(ids);
        return toResult(sysImGroupService.removeIds(list));
    }

    @Operation(summary = "群组新增用户信息")
    @PostMapping("/saveGroupUser")
    public Result saveGroupUser(@RequestBody SysImGroupUser imGroupUser) {
        imGroupUser.setUserId(SecurityUtils.getUserId());
        return sysImGroupService.saveGroupUser(imGroupUser);
    }

    @Operation(summary = "群组删除用户信息")
    @PostMapping("/delGroupUser")
    public Result delGroupUser(Long groupId) {
        SysImGroupUser imGroupUser = new SysImGroupUser();
        imGroupUser.setGroupId(groupId);
        imGroupUser.setUserId(SecurityUtils.getUserId());
        return sysImGroupService.delGroupUser(imGroupUser);
    }

    @Operation(summary = "查询群组用户列表")
    @GetMapping("/groupUserList")
    public Result groupUserList(Long groupId) {
        return Result.success("查询成功", sysImGroupService.getImGroupUserList(groupId));
    }

}
